home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: tank.news.pipex.net!pipex!warwick!bsmail!talisker!nathan
- From: nathan@pact.srf.ac.uk (Nathan Sidwell)
- Subject: Re: Override a cpp macro?
- Message-ID: <DL0onF.BF8@uns.bris.ac.uk>
- Sender: usenet@uns.bris.ac.uk (Usenet news owner)
- Nntp-Posting-Host: talisker.pact.srf.ac.uk
- Organization: Inmos
- X-Newsreader: TIN [version 1.2 PL2]
- References: <4d0v5r$6fs@oak78.doc.ic.ac.uk>
- Date: Thu, 11 Jan 1996 12:42:51 GMT
-
- Richard Jones (rwmj@doc.ic.ac.uk) wrote:
- : I'm trying to redefine a cpp macro, but using the old definition. I've got
- : a macro defined:
-
- : #define m1(a,b) /* some hidden definition of m1 */
- :[tried things like]
- : #define old_m1(a,b) m1(a,b)
- : #undef m1
- : #define m1(a,b) (old_m1(a,b)+1)
-
- : But neither works as expected. I've tried variations using the macro:
- you're using the wrong value of expected!
-
- : What's the way to do this? Surely this is an obvious thing to want to do,
- : so there must be some simple point I'm missing?
- No, you cannot do what you want with cpp. #define macros are simple
- text substitution (with arguments). what #define is saying is
- take the rest of the line (+continuations with \) and remember it. No
- interpretation happens at this point. Then when the #define name is
- encountered (outside of a #define), the name (and args) are replaced
- with the previously remembered text. (Note to pedants, I've ignored
- the concept of tokens here.) The substututed text is then rescanned
- for further #define substitutions.
-
- Substitutions in the body of the #define happen when the #define
- is being used, not when it is being defined.
-
- so if I have
- #define BUFFER 3
- #define BUFFER2 (BUFFER*2)
-
- the text for BUFFER2 is '(BUFFER*2)', it is not '(3*2)'.
-
- This does make it possible to have
-
- #define colour pink
- #define pink red
- colour
-
- expand to
- red
-
- rather than pink, which is what would happen if body expansion happened at
- definition. So the order of #defines is not important.
-
- : #define expand(a) a
-
- : which according to the Gnu info page for (Gnu) cpp should expand the
- : argument first. However, no known combination works.
-
- What this is saying is that when a #define has arguments, they are expanded
- before substitution into the macro body, not after. So
-
- #define FOO(arg) before arg after
- #define OTHER other_val
- FOO(OTHER)
-
- proceeds via the intermediate stage
- FOO(other_val)
- rather than the stage
- before OTHER after
- which would have been the case if arguments are expanded after
- substitution. (Things are different if the # or ## operators are used)
-
- nathan
- --
- Nathan Sidwell Holder of the Xmris home page
- Chameleon Architecture Group at SGS-Thomson, formerly Inmos
- http://www.pact.srf.ac.uk/~nathan/ Tel 0117 9707182
- nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
- Having problems? try http://www.pact.srf.ac.uk/~nathan/problems/
-